package com.yuetong.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.yuetong.business.mapper.YtElevatorInfoMapper;
import com.yuetong.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.yuetong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yuetong.business.domain.YtElevatorInfo;
import com.yuetong.business.mapper.YtPlaceInfoMapper;
import com.yuetong.business.domain.YtPlaceInfo;
import com.yuetong.business.service.IYtPlaceInfoService;

/**
 * 场所信息，用于存储各个场所的基本信息及其层级关系Service业务层处理
 *
 * @author  卓晓君
 * @date 2025-03-13
 */
@Service
public class YtPlaceInfoServiceImpl implements IYtPlaceInfoService
{
    @Autowired
    private YtPlaceInfoMapper ytPlaceInfoMapper;
    @Autowired
    private YtElevatorInfoMapper ytElevatorInfoMapper;

    /**
     * 查询场所信息，用于存储各个场所的基本信息及其层级关系
     *
     * @param placeId 场所信息，用于存储各个场所的基本信息及其层级关系主键
     * @return 场所信息，用于存储各个场所的基本信息及其层级关系
     */
    @Override
    public YtPlaceInfo selectYtPlaceInfoByPlaceId(Long placeId)
    {
        YtPlaceInfo placeInfo = ytPlaceInfoMapper.selectYtPlaceInfoByPlaceId(placeId);
        if (StringUtils.isNotNull(placeInfo))
        {
            List<YtElevatorInfo> ytElevatorInfoList = ytElevatorInfoMapper.selectYtElevatorInfoByPlaceId(placeId);
            placeInfo.setYtElevatorInfoList(ytElevatorInfoList);
                // 用法计算总保养费用
                BigDecimal totalMaintenanceCost = BigDecimal.ZERO;

                for (YtElevatorInfo elevatorInfo : ytElevatorInfoList) {
                    if (elevatorInfo.getMaintainAmount() != null) {
                        totalMaintenanceCost = totalMaintenanceCost.add(elevatorInfo.getMaintainAmount());
                    }
                }

                // 计算总金额并乘以 12 个月
                BigDecimal totalAmount = totalMaintenanceCost.multiply(new BigDecimal(12));
                placeInfo.setAmount(totalAmount);
            }

        return placeInfo;
    }

    /**
     * 查询场所信息，用于存储各个场所的基本信息及其层级关系列表
     *
     * @param ytPlaceInfo 场所信息，用于存储各个场所的基本信息及其层级关系
     * @return 场所信息，用于存储各个场所的基本信息及其层级关系
     */
    @Override
    public List<YtPlaceInfo> selectYtPlaceInfoList(YtPlaceInfo ytPlaceInfo)
    {
        List<YtPlaceInfo>   placeInfoList = ytPlaceInfoMapper.selectYtPlaceInfoList(ytPlaceInfo);
        for (YtPlaceInfo placeInfo : placeInfoList) {
            List<YtElevatorInfo> ytElevatorInfoList = ytElevatorInfoMapper.selectYtElevatorInfoByPlaceId(placeInfo.getPlaceId());
            if (ytElevatorInfoList != null) {
                int elevatorNum = ytElevatorInfoList.size();
                BigDecimal totalMaintenanceCost = BigDecimal.ZERO;

                for (YtElevatorInfo elevatorInfo : ytElevatorInfoList) {
                    if (elevatorInfo.getMaintainAmount() != null) {
                        totalMaintenanceCost = totalMaintenanceCost.add(elevatorInfo.getMaintainAmount());
                    }
                }

                // 计算总金额并乘以 12 个月
                BigDecimal totalAmount = totalMaintenanceCost.multiply(new BigDecimal(12));

                // 更新 placeInfo 的 elevatorNum 和 amount
                placeInfo.setElevatorNum(elevatorNum);
                placeInfo.setAmount(totalAmount);
            }
        }
        return placeInfoList;
    }

    /**
     * 新增场所信息，用于存储各个场所的基本信息及其层级关系
     *
     * @param ytPlaceInfo 场所信息，用于存储各个场所的基本信息及其层级关系
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYtPlaceInfo(YtPlaceInfo ytPlaceInfo)
    {
        ytPlaceInfo.setCreateTime(DateUtils.getNowDate());
        int rows = ytPlaceInfoMapper.insertYtPlaceInfo(ytPlaceInfo);
        insertYtElevatorInfo(ytPlaceInfo);
        return rows;
    }

    /**
     * 修改场所信息，用于存储各个场所的基本信息及其层级关系
     *
     * @param ytPlaceInfo 场所信息，用于存储各个场所的基本信息及其层级关系
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYtPlaceInfo(YtPlaceInfo ytPlaceInfo)
    {
        ytPlaceInfo.setUpdateTime(DateUtils.getNowDate());
        ytPlaceInfoMapper.deleteYtElevatorInfoByPlaceId(ytPlaceInfo.getPlaceId());
        insertYtElevatorInfo(ytPlaceInfo);
        return ytPlaceInfoMapper.updateYtPlaceInfo(ytPlaceInfo);
    }

    /**
     * 批量删除场所信息，用于存储各个场所的基本信息及其层级关系
     *
     * @param placeIds 需要删除的场所信息，用于存储各个场所的基本信息及其层级关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtPlaceInfoByPlaceIds(Long[] placeIds)
    {
        ytPlaceInfoMapper.deleteYtElevatorInfoByPlaceIds(placeIds);
        return ytPlaceInfoMapper.deleteYtPlaceInfoByPlaceIds(placeIds);
    }

    /**
     * 删除场所信息，用于存储各个场所的基本信息及其层级关系信息
     *
     * @param placeId 场所信息，用于存储各个场所的基本信息及其层级关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtPlaceInfoByPlaceId(Long placeId)
    {
        ytPlaceInfoMapper.deleteYtElevatorInfoByPlaceId(placeId);
        return ytPlaceInfoMapper.deleteYtPlaceInfoByPlaceId(placeId);
    }
    //已签约总数
    @Override
    public int getSignedCount(String signedStaute) {
        return ytPlaceInfoMapper.selectSingnedCount(signedStaute);
    }

    /**
     * 新增电梯信息，用于存储电梯的基本信息及其所属场所信息
     *
     * @param ytPlaceInfo 场所信息，用于存储各个场所的基本信息及其层级关系对象
     */
    public void insertYtElevatorInfo(YtPlaceInfo ytPlaceInfo)
    {
        List<YtElevatorInfo> ytElevatorInfoList = ytPlaceInfo.getYtElevatorInfoList();
        Long placeId = ytPlaceInfo.getPlaceId();
        if (StringUtils.isNotNull(ytElevatorInfoList))
        {
            List<YtElevatorInfo> list = new ArrayList<YtElevatorInfo>();
            for (YtElevatorInfo ytElevatorInfo : ytElevatorInfoList)
            {
                ytElevatorInfo.setPlaceId(placeId);
                list.add(ytElevatorInfo);
            }
            if (list.size() > 0)
            {
                ytPlaceInfoMapper.batchYtElevatorInfo(list);
            }
        }
    }
}
